package com.leaves.common.util.docutil;

/**
 * @Author LEAVES
 * @Version 1.0
 * @Date 2023年07日27 00时26分38秒
 * @Description:
 */
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.IOUtils;

import java.io.*;

public class WordTableWriter {

    public static void main(String[] args) {
        String templatePath = "D:\\Program Files (x86)\\app\\IDEA\\IDEA_WORKE\\sa-token-demo\\src\\main\\resources\\templates\\templates/行车日志.docx";
        String outputPath = "D:\\Program Files (x86)\\app\\IDEA\\IDEA_WORKE\\sa-token-demo\\src\\main\\resources\\templates/result/行车日志.docx";


        try {
            // 读取模板文件
            FileInputStream inputStream = new FileInputStream(templatePath);
            XWPFDocument document = new XWPFDocument(inputStream);

            // 获取表格
            XWPFTable table = document.getTables().get(0); // 假设表格在文档的第一个位置

            // 写入数据
            table.getRow(0).getCell(3).setText("2023年07月27日");
            table.getRow(1).getCell(1).setText("测试计划");
            table.getRow(1).getCell(3).setText("陕AA789Q");
            table.getRow(1).getCell(5).setText("西安-渭南");
            table.getRow(2).getCell(5).setText("陕西天地众力能源发展有限公司华州区分公司");

            // 插入图片
            //String imagePath = "path/to/your/image.png";
            //insertImage(document, table.getRow(1).getCell(2), imagePath);

            // 保存输出文件
            FileOutputStream outputStream = new FileOutputStream(outputPath);
            document.write(outputStream);

            // 关闭流
            outputStream.close();
            inputStream.close();
            document.close();

            System.out.println("操作完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void insertImage(XWPFDocument doc, XWPFTableCell cell, String imagePath) {
        try {
            File imageFile = new File(imagePath);
            FileInputStream imageStream = new FileInputStream(imageFile);
            byte[] imageBytes = IOUtils.toByteArray(imageStream);
            imageStream.close();

            int format;
            if (imagePath.endsWith(".png")) {
                format = XWPFDocument.PICTURE_TYPE_PNG;
            } else if (imagePath.endsWith(".jpg") || imagePath.endsWith(".jpeg")) {
                format = XWPFDocument.PICTURE_TYPE_JPEG;
            } else {
                throw new IllegalArgumentException("Unsupported image file format.");
            }

            XWPFParagraph paragraph = cell.getParagraphs().get(0);
            XWPFRun run = paragraph.createRun();
            try {
                run.addPicture(new ByteArrayInputStream(imageBytes), format, imageFile.getName(), Units.toEMU(100), Units.toEMU(100));
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

